import QtQuick 2.15
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.15
import './../../../controls'
Popup {
    id:pop
    width: 800
    height: 400
    modal: true
    anchors.centerIn: parent
    property var callBackFunc: null
    property var amount: 0
    property var bno: ''
    property var bname: ''
    property var pWin: null
    property bool isdeposit:false // 标记是否是 押金
    property int  payType: -1 //0--微信，1--支付宝 2--现金
    property bool isBusying: false

    closePolicy:Popup.NoAutoClose
    background:BorderImage {
        anchors.fill: parent
        anchors.leftMargin: -16
        anchors.rightMargin: -16
        anchors.topMargin: -12
        anchors.bottomMargin: -20
        border.left: 16
        border.right: 16
        border.top: 12
        border.bottom: 20
        source: "qrc:/images/DialogFrame3.png"
    }
    onClosed:{
        pop.destroy()
    }
    onOpened: {
        if(amount === 0)
        {
            payType=2;
            itemSelect.visible=false
            itemMorey.visible=true
        }
    }

    ButtonGroup{
        id:groupButton
    }


    Connections{
        target: ScannerHelper
        function onRecvScannerData(data){
            recvScannerData(data)
        }
    }

    function recvScannerData(str)
    {
        if(itemPayLine.visible || isBusying)
        {
            return
        }

        //收到扫码枪数据
        if(payType < 0)
        {
            CGlobal.showMessage(window,qsTr('请选择支付方式'))
            return
        }

        var request = HttpHelper.createIMRequest();
        // request.enabledLog=true
        request.onSucceed( function(request){
            itemPayLine.visible=true
            itemSelect.visible=false
            isBusying = false;
        } );
        request.onError(  function(request){
            isBusying = false;
            pop.close()
            CGlobal.showMessage(window,request.errorMessage)
        } );
        isBusying = true;
        if(isdeposit)
        {
            //押金 缴纳完成
            request.post("/api/common/micropay",
                         {
                             bno:pop.bno,
                             auth_code:str,
                             type:payType
                         })
        }
        else
        {
            //结算完成
            request.post("/api/common/micropay_goods",
                         {
                             bno:pop.bno,
                             auth_code:str,
                             type:payType
                         })
        }
    }

    contentItem: Item {
        anchors.fill: parent
        anchors.margins: 20
        Item {
            id:itemSelect
            anchors.fill: parent
            Row{
                anchors.centerIn: parent
                spacing: 50
                Button{
                    width: 130
                    height: 130
                    checkable: true
                    visible: false
                    ButtonGroup.group: groupButton
                    background:Rectangle{
                        radius: 65
                        border.color: parent.checked ? 'green' :'transparent'
                        Image {
                            source: "qrc:/images/zfb.png"
                            anchors.fill: parent
                            anchors.margins: 4
                        }
                    }
                    onClicked: {
                        payType=1
                    }
                    Text {
                        anchors.top:  parent.bottom
                        anchors.topMargin: 16
                        text: qsTr("支付宝")
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
                }
                Button{
                    width: 130
                    height: 130
                    checkable: true
                    ButtonGroup.group: groupButton
                    background:Rectangle{
                        radius: 65
                        border.color: parent.checked ? 'green' :'transparent'
                        Image {
                            source: "qrc:/images/vx.png"
                            anchors.fill: parent
                            anchors.margins: 4
                        }
                    }
                    onClicked: {
                        payType=0
                    }

                    Text {
                        anchors.top:  parent.bottom
                        anchors.topMargin: 16
                        text: qsTr("微信")
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
                }
                Button{
                    width: 130
                    height: 130
                    checkable: true
                    background: Rectangle{
                        radius: 65
                        Image {
                            source: "qrc:/images/xj.png"
                            anchors.centerIn: parent
                            anchors.margins: 4
                        }
                    }
                    onClicked: {
                        payType=2;
                        itemSelect.visible=false
                        itemMorey.visible=true
                    }
                    Text {
                        anchors.top:  parent.bottom
                        anchors.topMargin: 16
                        text: qsTr("现金")
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
                }
            }
            TextField{
                id:textKey
                height: 42
                width: 300
                visible: payType === 0 || payType === 1
                font.pixelSize: 14
                color: '#333333'
                focus: true
                placeholderText: qsTr('请输入支付码')
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 16
                selectByMouse: true
                background: Rectangle{
                    border.color: '#dddddd'
                }

                Keys.onReturnPressed: {
                    btnQuery.clicked()
                }
                Keys.onEnterPressed: {
                    btnQuery.clicked()
                }
                Button{
                    id:btnQuery
                    width: 80
                    height: 40
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.right
                    anchors.leftMargin: 10
                    focusPolicy:Qt.NoFocus
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("确定")
                        }
                    }
                    onClicked: {
                        if(textKey.text.length === 0)
                        {
                            CGlobal.showMessage(window,'请输入支付码')
                            return
                        }
                        recvScannerData( textKey.text)
                        textKey.clear()
                    }
                }
            }
        }
        Item {
            id:itemPayLine
            anchors.fill: parent
            visible: false
            Text {
                color: '#333333'
                text: qsTr("正在等待支付返回结果")
                anchors.centerIn: parent
            }
            Timer{
                id:timerQuery
                running: parent.visible
                repeat: true
                interval: 2000
                onTriggered: {
                    console.log('-------begin water_query_statu------')
                    timerQuery.stop()
                    var request = HttpHelper.createIMRequest();
                    // request.enabledLog=true
                    request.onSucceed( function(request){
                        console.log('----water_query_statu---',JSON.stringify(request.responseData))
                        if(request.responseData.status ===1)
                        {
                            //支付完成
                            pop.close()
                            if(callBackFunc)
                            {
                                callBackFunc(bno)
                            }
                            CGlobal.showMessage(window,qsTr('支付完成'))
                            return
                        }
                        else if(request.responseData.status === 0)
                        {
                           //支付中
                        }
                        else
                        {
                            CGlobal.showMessage(window,qsTr('支付失败'))
                            pop.close()
                            return
                        }
                        timerQuery.start()
                    } );
                    request.onError(  function(request){
                        console.log('----water_query_statu--error-',JSON.stringify(request.errorMessage))
                        timerQuery.stop()
                        pop.close()
                        CGlobal.showMessage(window,request.errorMessage)
                    } );
                    if(isdeposit)
                    {
                        request.get("/api/common/query_micropay_order?bno="+pop.bno)
                    }
                    else
                    {
                        request.get("/api/common/query_micropay_order_goods?bno="+pop.bno)
                    }
                }
            }
        }
        Item {
            id:itemMorey
            anchors.fill: parent
            visible: false
            Column{
                anchors.centerIn: parent
                spacing: 50
                Text {
                    color: '#333333'
                    text: qsTr("点击现金支付，并等待支付结果")
                    anchors.horizontalCenter: parent.horizontalCenter
                }
                Button{
                    id:btnPay
                    width: 160
                    height: 40
                    anchors.horizontalCenter: parent.horizontalCenter
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("现金支付")
                        }
                    }
                    onClicked: {
                        recvScannerData('1')
                        itemMorey.visible=false
                    }
                }
            }
        }
        Text {
            anchors.horizontalCenter: parent.horizontalCenter
            y:10
            font.pixelSize: 24
            font.bold: true
            color: '#E42727'
            text: (isdeposit ? qsTr('押金') : qsTr('结算')) + ' ' + qsTr("￥")+amount.toFixed(2)+qsTr('元')
        }
        Text {
            anchors.top: parent.top
            anchors.topMargin: 50
            font.pixelSize: 14
            color: '#666666'
            anchors.horizontalCenter: parent.horizontalCenter
            text: qsTr('手环号: ')+ pop.bname
        }
        Button{
            width: 32
            height: 32
            anchors.right: parent.right
            //            visible: false
            background: Image {
                anchors.centerIn: parent
                source: "qrc:/images/close.png"
            }
            onClicked: {
                pop.close()
            }
        }
    }
}
